﻿using Saisie_des_prélèvements.Models;
using System;
using System.Linq;

namespace Saisie_des_prélèvements.ViewModels
{
    public class UserViewModel : ViewModelBase
    {
        #region Properties

        private int user_id = 0;
        public int UserId
        {
            get
            { return user_id; }

            set
            {
                if (user_id == value)
                { return; }

                user_id = value;
                RaisePropertyChanged("UserId");
            }
        }

        private string user_login = string.Empty;
        public string Login
        {
            get
            { return user_login; }

            set
            {
                if (user_login == value)
                { return; }

                user_login = value;
                RaisePropertyChanged("Login");
            }
        }

        private string user_pwd = string.Empty;
        public string Password
        {
            get
            { return user_pwd; }

            set
            {
                if (user_pwd == value)
                { return; }

                user_pwd = value;
                RaisePropertyChanged("Password");
            }
        }

        private bool? user_is_admin = false;
        public bool? IsAdmin
        {
            get
            { return user_is_admin; }

            set
            {
                if (user_is_admin == value)
                { return; }

                user_is_admin = value;
                RaisePropertyChanged("IsAdmin");
            }
        }

        private bool? user_is_tablet = false;
        public bool? IsTablet
        {
            get
            { return user_is_tablet; }

            set
            {
                if (user_is_tablet == value)
                { return; }

                user_is_tablet = value;
                RaisePropertyChanged("IsTablet");
            }
        }

        private bool? user_can_create_contract = false;
        public bool? CanContract
        {
            get
            { return user_can_create_contract; }

            set
            {
                if (user_can_create_contract == value)
                { return; }

                user_can_create_contract = value;
                RaisePropertyChanged("CanContract");
            }
        }

        private bool? user_can_update_calypso = false;
        public bool? CanCalypso
        {
            get
            { return user_can_update_calypso; }

            set
            {
                if (user_can_update_calypso == value)
                { return; }

                user_can_update_calypso = value;
                RaisePropertyChanged("CanCalypso");
            }
        }

        private string user_first_name = string.Empty;
        public string FirstName
        {
            get
            { return user_first_name; }

            set
            {
                if (user_first_name == value)
                { return; }

                user_first_name = value;
                RaisePropertyChanged("FirstName");
            }
        }

        private string user_last_name = string.Empty;
        public string LastName
        {
            get
            { return user_last_name; }

            set
            {
                if (user_last_name == value)
                { return; }

                user_last_name = value;
                RaisePropertyChanged("LastName");
            }
        }

        private string user_mail = string.Empty;
        public string Mail
        {
            get
            { return user_mail; }

            set
            {
                if (user_mail == value)
                { return; }

                user_mail = value;
                RaisePropertyChanged("Mail");
            }
        }

        private string user_phone = string.Empty;
        public string Phone
        {
            get
            { return user_phone; }

            set
            {
                if (user_phone == value)
                { return; }

                user_phone = value;
                RaisePropertyChanged("Phone");
            }
        }

        private int? user_create = 0;
        public int? UserCreate
        {
            get
            { return user_create; }

            set
            {
                if (user_create == value)
                { return; }

                user_create = value;
                RaisePropertyChanged("UserCreate");
            }
        }

        private DateTime? date_create = System.DateTime.Today;
        public DateTime? DateCreate
        {
            get
            { return date_create; }

            set
            {
                if (date_create == value)
                { return; }

                date_create = value;
                RaisePropertyChanged("DateCreate");
            }
        }

        #endregion "Properties"

        public UserViewModel GetUser(int userId)
        {
            var user = new UserViewModel();
            using (var db = new SQLite.SQLiteConnection(App.DBPath))
            {
                var _user = (db.Table<k_user>().Where(
                    c => c.user_id == userId)).Single();
                user.UserId = _user.user_id;
                user.Login = _user.user_login;
                user.Password = _user.user_pwd;
                user.IsAdmin = _user.user_is_admin;
                user.IsTablet = _user.user_is_tablet;
                user.CanContract = _user.user_can_create_contract;
                user.CanCalypso = _user.user_can_update_calypso;
                user.FirstName = _user.user_first_name;
                user.LastName = _user.user_last_name;
                user.Mail = _user.user_mail;
                user.Phone = _user.user_phone;
                user.UserCreate = _user.user_create;
                user.DateCreate = _user.date_create;
            }
            return user;
        }

        public int GetUserID(string user_login, string user_pwd)
        {
            int userID = 0;
            try
            {
                using (var db = new SQLite.SQLiteConnection(App.DBPath))
                {
                    var user = (db.Table<k_user>().Where(
                        c => c.user_login == user_login && c.user_pwd == user_pwd)).Single();
                    userID = user.user_id;
                }

                return userID;
            }
            catch
            {
                return 0;
            }
            
        }
    }
}
